import config from '@/config/config.js';
import axios from 'axios';

const workstationRequest = {
    loadWorkstationList(searchWorkstation, callback){
        const url = config.mdBaseUrl + '/workstation/list';
        axios.get(url, {
            params: {
                workstationName: searchWorkstation.workstationName || null,
                workstationCode: searchWorkstation.workstationCode || null,
                workshopName: searchWorkstation.workshopName || null,
                processName: searchWorkstation.processName || null,
            },
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // 假设返回结果中有 code 字段来标识成功状态
                callback(res.data);
            } else {
                console.error('请求失败:', res.data.message);
            }
        }).catch(error => {
            console.error('API请求出错:', error);
        });
    },
    insertWorkstation(workstation, callback){
        const url = config.mdBaseUrl + '/workstation/add';
        axios.post(url, workstation, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // 假设返回结果中有 code 字段来标识成功状态
                callback(res.data);
            } else if (res.data.msg == "OK") {
                callback(res.data);
            }else {
                    console.error('请求失败:', res.data.message);
                }

        });
    },
    updateWorkstation(workstation, callback){
        const url = config.mdBaseUrl + '/workstation/update';
        axios.post(url, workstation, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // 假设返回结果中有 code 字段来标识成功状态
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }else {
                console.error('请求失败:', res.data.message);
            }
        });
    },
    deleteWorkshop(workstationId, callback){
        const url = config.mdBaseUrl + '/workstation/delete?workstationId=' + workstationId;
        axios.delete(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }else {
                console.error('请求失败:', res.data.message);
            }
        });
    },
    queryTools(workstationId, callback){
        const url = config.mdBaseUrl + '/workstation/queryTools?workstationId=' + workstationId;
        axios.get(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    queryWorkers(workstationId, callback){
        const url = config.mdBaseUrl + '/workstation/queryWorkers?workstationId=' + workstationId;
        axios.get(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    queryMachines(workstationId, callback){
        const url = config.mdBaseUrl + '/workstation/queryMachines?workstationId=' + workstationId;
        axios.get(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    handleDeleteMachine(machineId, callback){
        const url = config.mdBaseUrl + '/workstation/handleDeleteMachine?machineId=' + machineId;
        axios.delete(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    handleDeleteWorker(workerId, callback){
        const url = config.mdBaseUrl + '/workstation/handleDeleteWorker?workerId=' + workerId;
        axios.delete(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    handleDeleteTool(toolId, callback){
        const url = config.mdBaseUrl + '/workstation/handleDeleteTool?toolId=' + toolId;
        axios.delete(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if (res.data.msg == "OK") {
                callback(res.data);
            }
        })
    },
    insertToolWorkstation(Tool, callback){
        const url = config.mdBaseUrl + '/workstation/insertToolWorkstation';
        axios.post(url, Tool, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if(res.data.msg == "OK"){
                callback(res.data);
            }else {
                console.error('请求失败:', res.data.message);
            }
        });
    },
    insertWorker(Worker, callback){
        const url = config.mdBaseUrl + '/workstation/insertWorker';
        axios.post(url, Worker, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if(res.data.msg == "OK"){
                callback(res.data);
            }else {
                console.error('请求失败:', res.data.message);
            }
        });
    },
    addMachine(Machine, callback){
        const url = config.mdBaseUrl + '/workstation/addMachine';
        axios.post(url, Machine, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {
                callback(res.data);
            }else if(res.data.msg == "OK"){
                callback(res.data);
            }else {
                console.error('请求失败:', res.data.message);
            }
        });
    }

};

export default workstationRequest;